package util

import (
	"fmt"
	"io"
	"mime/multipart"
	"os"
)

const dir string = "data"

func Write_local(inputfile *multipart.FileHeader, fName string) error {
	// 确保目录存在
	if err := os.MkdirAll(dir, os.ModePerm); err != nil {
		fmt.Println("创建目录失败:", err)
		return err
	}
	buf := make([]byte, inputfile.Size)
	r, err := inputfile.Open()
	if err != nil {
		return err
	}
	defer r.Close()
	// 创建文件
	filePath := dir + "/" + fName + ".jpg"
	w, err := os.Create(filePath)
	if err != nil {
		fmt.Println("创建文件失败:", err)
		return err
	}
	defer w.Close()
	_, err = io.CopyBuffer(w, r, buf)
	if err != nil {
		return err
	}
	return nil
}
